﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using Payroll.Common.Utils;
using PayrollHR.Web;

namespace PayrollHR.Administration.Modules
{
    public partial class RelationshipControl : BasePayrollAdministrationUserControl
    {
        public int CustomerID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.FillCustType();
                this.FillMonthDropDowns();
                this.FillYearDropDowns();
                this.FillYearOfBirthDropDowns();
                this.FillContriesDropDowns();
                this.BindGrid();
            }
        }
        private void FillYearOfBirthDropDowns()
        {
            ddlYearOfBrirth.Items.Clear();
            int Min = DateTime.Now.Year - 60;
            int Max = DateTime.Now.Year - 10;
            ddlYearOfBrirth.Items.Add(new ListItem("-None-", "0"));
            for (int i = Max; i >= Min; i--)
            {
                ListItem ddlOfBrirthItem2 = new ListItem(CommonHelper.ToString(i), CommonHelper.ToString(i));
                ddlYearOfBrirth.Items.Add(ddlOfBrirthItem2);
            }
            ddlYearOfBrirth.SelectedIndex = 0;
        }
        private void FillYearDropDowns()
        {
            ddlYear.Items.Clear();
            int year = DateTime.Now.Year;
            ddlYear.Items.Add(new ListItem("-None-", "0"));
            for (int i = year; i >= year - 4; i--)
            {
                ListItem ddlYearItem2 = new ListItem(CommonHelper.ToString(i), CommonHelper.ToString(i));
                ddlYear.Items.Add(ddlYearItem2);
            }
            ddlYear.SelectedIndex = 0;
        }
        private void FillMonthDropDowns()
        {
            ddlMonth.Items.Clear();
            ddlMonth.Items.Add(new ListItem("-None-", "0"));
            ddlMonthOfBrirth.Items.Clear();
            ddlMonthOfBrirth.Items.Add(new ListItem("-None-", "0"));
            for (int i = 1; i <= 12; i++)
            {
                ListItem ddlMonthItem2 = new ListItem(CommonHelper.ToString(i), CommonHelper.ToString(i));
                ddlMonth.Items.Add(ddlMonthItem2);
                ddlMonthOfBrirth.Items.Add(ddlMonthItem2);
            }
            ddlMonth.SelectedIndex = 0;
        }
        private void FillContriesDropDowns()
        {
            ddlNational.Items.Clear();
            var languageCollection = this.CustomerService.GetObjectById(1);
            if (languageCollection.Count != 1)
            {
                languageCollection.Insert(0, new Objects { ObjectId = 0, ObjectName = "-None-" });
            }
            foreach (Objects language in languageCollection)
            {
                ListItem ddlChannelItem2 = new ListItem(language.ObjectName, language.ObjectId.ToString());
                ddlNational.Items.Add(ddlChannelItem2);
            }
        }
        public void FillCustType()
        {
            ddlCustType.Items.Clear();
            var customertypeSelection = this.CustomerService.GetCustomerType();
            customertypeSelection.Insert(0, new CutomerType { CutomerTypeId = 0, CutomerTypeName = "All" });
            foreach (var customertype in customertypeSelection)
            {
                ListItem ddlCustTypeItem2 = new ListItem(customertype.CutomerTypeName, customertype.CutomerTypeId.ToString());
                ddlCustType.Items.Add(ddlCustTypeItem2);
            }
        }
        protected void BindCustomer()
        {
            int customerTypeId = 0;
            int CustNationalId = 0;
            int Year = 0;
            int Month = 0;
            if (ddlCustType.SelectedIndex > -1)
            {
                customerTypeId = int.Parse(ddlCustType.SelectedItem.Value);
            }
            if (ddlNational.SelectedIndex > -1)
            {
                CustNationalId = int.Parse(ddlNational.SelectedItem.Value);
            }
            if (ddlYear.SelectedIndex > 0)
            {
                Year = CommonHelper.ToInteger(ddlYear.SelectedItem.Value);
            }
            if (ddlMonth.SelectedIndex > 0)
            {
                Month = CommonHelper.ToInteger(ddlMonth.SelectedItem.Value);
            }
            string CustYearOfBirth = "0";
            if (ddlYearOfBrirth.SelectedIndex > 0)
            {
                CustYearOfBirth = ddlYearOfBrirth.SelectedItem.Value;
            }
            string CustMonthOfBirth = "0";
            if (ddlMonthOfBrirth.SelectedIndex > 0)
            {
                CustMonthOfBirth = ddlMonthOfBrirth.SelectedItem.Value;
            }
            CustomerSearchResult customer = new CustomerSearchResult
            {
                CustName = txtCustomerName.Text.Trim(),
                CustCaseWork = txtCustCaseWork.Text.Trim(),
                CustHour = txtCustHour.Text.Trim(),
                CustMobile = txtMobile.Text.Trim(),
                CustYearOfBirth = CustYearOfBirth,
                CustMonthOfBirth = CustMonthOfBirth,
                CustParent = txtCustParent.Text.Trim(),
                CustNationalId = CustNationalId,
                //CustCounselor = txtCounselor.Text.Trim(),
                CutomerTypeId = customerTypeId,
                //ProCurator = txtProCurator.Text.Trim(),
                CustYear = Year,
                CustMonth = Month,
                UserId = 0
            };
            var custCplection = CustomerService.GetNotRelationship(customer, this.CustomerID);
            gvCustomerAdd.DataSource = custCplection;
            gvCustomerAdd.DataBind();
        }
        protected void BindGrid()
        {
            gvCustomerRelationship.DataSource = Relationships();
            gvCustomerRelationship.DataBind();
        }
        protected List<Relationships> Relationships()
        {
            List<Relationships> relation = CustomerService.GetListRelationship(this.CustomerID);
            return relation;
        }
        /// <summary>
        /// Gets the customer.
        /// </summary>
        /// <param name="List">The list.</param>
        protected void GetCustomer(ref List<Customer> List)
        {
            foreach (GridViewRow row in gvCustomerAdd.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    string CustCaseWork = (row.Cells[1].FindControl("lblCustCaseWork") as Label).Text;
                    string CustName = (row.Cells[2].FindControl("lblCustName") as Label).Text;
                    string ProCurator = (row.Cells[3].FindControl("lblProCurator") as Label).Text;
                    string CustCounselor = (row.Cells[4].FindControl("lblCustCounselor") as Label).Text;
                    string CustMobile = (row.Cells[5].FindControl("lblCustMobile") as Label).Text;
                    string customerid = gvCustomerAdd.DataKeys[row.RowIndex].Value.ToString();
                    List.Add(new Customer
                    {
                        CustomerID = CommonHelper.ToInteger(customerid),
                        CustCaseWork = CustCaseWork,
                        CustName = CustName,
                        CustMobile = CustMobile,
                        ProCurator = ProCurator,
                        CustCounselor = CustCounselor
                    });
                }
            }
        }
        public void Relationships(ref List<Relationships> List)
        {
            foreach (GridViewRow row in gvCustomerRelationship.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    int RelationshipID = CommonHelper.ToInteger(gvCustomerRelationship.DataKeys[row.RowIndex].Values[0]);
                    int CustomerID = CommonHelper.ToInteger(gvCustomerRelationship.DataKeys[row.RowIndex].Values[1]);
                    string CustCaseWork = (row.Cells[0].FindControl("ActionHyperLink") as HyperLink).Text;
                    string CustName = (row.Cells[1].FindControl("caseworkHyperLink") as HyperLink).Text;
                    string ProCurator = (row.Cells[2].FindControl("lblProCurator") as Label).Text;
                    string CustCounselor = (row.Cells[3].FindControl("lblCustCounselor") as Label).Text;
                    string CustMobile = (row.Cells[4].FindControl("lblCustMobile") as Label).Text;
                    List.Add(new Relationships { 
                        RelationshipID = RelationshipID, 
                        CustomerID = CustomerID,
                        CustCaseWork = CustCaseWork,
                        CustName = CustName,
                        CustMobile = CustMobile,
                        ProCurator = ProCurator,
                        CustCounselor = CustCounselor
                    });
                }
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindCustomer();
        }
               
        protected void btnOk_Click(object sender, EventArgs e)
        {
            int relationshipID = this.CustomerID;
            List<Relationships> List = new List<Relationships>();
            List<Customer> ListNotSelect = new List<Customer>();
            this.Relationships(ref List);
            foreach (GridViewRow row in gvCustomerAdd.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                    string CustCaseWork = (row.Cells[1].FindControl("lblCustCaseWork") as Label).Text;
                    string CustName = (row.Cells[2].FindControl("lblCustName") as Label).Text;
                    string ProCurator = (row.Cells[3].FindControl("lblProCurator") as Label).Text;
                    string CustCounselor = (row.Cells[4].FindControl("lblCustCounselor") as Label).Text;
                    string CustMobile = (row.Cells[5].FindControl("lblCustMobile") as Label).Text;
                    string customerid = gvCustomerAdd.DataKeys[row.RowIndex].Value.ToString();
                    if (chkRow.Checked)
                    {
                        List.Add(new Relationships
                        {
                            RelationshipID = relationshipID,
                            CustomerID = CommonHelper.ToInteger(customerid),
                            CustCaseWork = CustCaseWork,
                            CustName = CustName,
                            CustMobile = CustMobile,
                            ProCurator = ProCurator,
                            CustCounselor = CustCounselor
                        });
                    }
                    else {
                        ListNotSelect.Add(new Customer
                        {
                            CustomerID = CommonHelper.ToInteger(customerid),
                            CustCaseWork = CustCaseWork,
                            CustName = CustName,
                            CustMobile = CustMobile,
                            ProCurator = ProCurator,
                            CustCounselor = CustCounselor
                        });
                    }
                }
            }
            gvCustomerAdd.DataSource = ListNotSelect;
            gvCustomerAdd.DataBind();
            gvCustomerRelationship.DataSource = List;
            gvCustomerRelationship.DataBind();
        }

        protected void gvCustomerRelationship_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = Convert.ToInt32(e.RowIndex);
            List<Relationships> List = new List<Relationships>();
            List<Customer> ListCust = new List<Customer>();
            this.Relationships(ref List);
            this.GetCustomer(ref ListCust);
            Relationships item = List[index];
            ListCust.Add(new Customer {
                CustomerID = item.CustomerID,
                CustCaseWork = item.CustCaseWork,
                CustName = item.CustName,
                CustMobile = item.CustMobile,
                ProCurator = item.ProCurator,
                CustCounselor = item.CustCounselor
            });
            List.RemoveAt(index);
            gvCustomerAdd.DataSource = ListCust;
            gvCustomerAdd.DataBind();
            gvCustomerRelationship.DataSource = List;
            gvCustomerRelationship.DataBind();
        }
    }
} 